﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;

namespace eGrid_App
{
    public class TroubleTicketServerLayer : FeatureLayer, ClonableLayer
    {
                
        public delegate void TicketSeverityChangedDelegate(int ticketID, int severity);                
        public event TicketSeverityChangedDelegate TicketSeverity;

        private ScatterLayer scatterlayer;

        public TroubleTicketServerLayer(ScatterLayer sl) : base()
        {
            scatterlayer = sl;

            ID = "ServerTickets";
            Opacity = 1;
            Visible = true;
            Url = "http://asebeast.cpsc.ucalgary.ca:1892/ArcGIS/rest/services/eGridOnline/MapServer/1";
            OutFields.Add("Description");
            OutFields.Add("Severity");
            OutFields.Add("OBJECTID");
            UpdateCompleted += serverTicketsUpdateCompleted;
            MapLayersManager.Instance.AddLayer(this);
        }

        public void serverTicketsUpdateCompleted(Object sender, EventArgs e)
        {
             MapLayersManager.Instance.AddLayer(CloneLayer());
        }

        public Layer CloneLayer()
        {
            TroubleTicketGraphicLayer ticketsLayer = new TroubleTicketGraphicLayer();
                ticketsLayer.Visible = true;
                ticketsLayer.Opacity = 1;
                ticketsLayer.ID = "TroubleTicketsLayer";
                ticketsLayer.tickets = new Dictionary<int, TroubleTicket>();

                //ticketsLayer.severityDelegate = new TroubleTicketServerLayer.TicketSeverityChangedDelegate(ticketsLayer.ChangeTicketGraphic);
                TicketSeverity += ticketsLayer.ChangeTicketGraphic; //ticketsLayer.severityDelegate;

                foreach (Graphic g in Graphics)
                {
                    TroubleTicket newTicket = new TroubleTicket(scatterlayer);
                    newTicket.Geometry = Geometry.Clone(g.Geometry);
                    newTicket.MouseLeftButtonUp += troubleTicketLeftClick;
                    newTicket.Priority = (int)g.Attributes["Severity"];
                    newTicket.originalGraphic = g;

                    ticketsLayer.Graphics.Add(newTicket);
                    ticketsLayer.tickets.Add((int)g.Attributes["OBJECTID"], newTicket);
                }

                return ticketsLayer;
        }


        public void TicketSeverityChanged(int ticketID, int severity)
        {
            if (TicketSeverity != null)
            {
                TicketSeverity(ticketID, severity);
            }
        }

        public void troubleTicketLeftClick(Object sender, EventArgs e)
        {
            TroubleTicket g = (TroubleTicket)sender;
            FeatureInfoTip fit = new FeatureInfoTip(scatterlayer, g, this);

            Point point = new Point();
            if (scatterlayer.BackgroundMap == null)
            {
                point.Y = SystemParameters.VirtualScreenWidth / 2;
                point.X = SystemParameters.VirtualScreenHeight / 2;
            }
            else
            {
                point = scatterlayer.BackgroundMap.MyMap.MapToScreen((MapPoint)g.Geometry);
            }
            scatterlayer.AddFeatureTip(fit, point, 0.0);
        }

    }
}
